﻿/*****************************************************************//**
 * @file    FITKGraphObjectInteractionBase.h
 * @brief   Graph object base for interactions and constraint.
 *          P.A.T.
 *          Some of the graph object may have different styles
 *          between release version and debug version.
 *
 * @author  ChengHaotian (yeguangbaozi@foxmail.com)
 * @date    2024-04-26
 *********************************************************************/

#ifndef __FITKGRAPHOBJECTINTERACTIONBASE_H__
#define __FITKGRAPHOBJECTINTERACTIONBASE_H__

#include "FITKGraphObjectMarkBase.h"

#include "FITKVTKGraphAdaptorAPI.h"

 // Forward declaration
class vtkPoints;
class vtkCellArray;
class vtkPolyData;
class vtkUnstructuredGrid;
class vtkGlyph3D;
class vtkDistanceToCamera;
class vtkRenderer;
class vtkSelectionNode;
class vtkSelection;
class vtkExtractSelection;
class vtkIdTypeArray;
class vtkIntArray;
class vtkCell;
class vtkPointSource;
class vtkMaskPoints;

class FITKGraphActor;
class FITKPolygonSource;

namespace Interface
{
    class FITKBoundaryLocation;
    class FITKInteractionLocation;
    class FITKModelSet;
}

namespace Exchange
{
    /**
     * @brief   Graph object base for interactions and constraint.
     * @author  ChengHaotian (yeguangbaozi@foxmail.com)
     * @date    2024-04-26
     */
    class FITKVTKGRAPHADAPTORAPI FITKGraphObjectInteractionBase : public FITKGraphObjectMarkBase
    {
        // Regist
        FITKGraphObjectRegist(FITKGraphObjectInteractionBase, FITKGraphObjectMarkBase);
        FITKCLASS(Exchange, FITKGraphObjectInteractionBase);

    public:
        /**
         * @brief   Constructor.
         * @param   caseData: The case data
         * @param   interactionsData: The interactions or constraint data object
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        FITKGraphObjectInteractionBase(AbaqusData::FITKDataCase* caseData, Interface::FITKInteractionLocation* interactionsData);

        /**
         * @brief   Destructor.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual ~FITKGraphObjectInteractionBase();

        /**
         * @brief   Show or hide graph object.[virtual][override]
         * @param   visibility: The visibility of graph object
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-08
         */
        virtual void setVisible(bool visibility) override;

        /**
         * @brief   Highlight the interactions or constraint graph actors.[virtual][override]
         * @param   color: The given color for highlighting this time[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void highlight(QColor color = QColor()) override;

        /**
         * @brief   Dis-highlight the interactions or constraint graph actors.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void disHighlight() override;

        /**
         * @brief   Update all graph actors.[virtual][override]
         * @param   forceUpdate: Force update[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void update(bool forceUpdate = false) override;

        /**
         * @brief   Update the visibility with data object's visibility.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        virtual void updateVisibility() override;

        /**
         * @brief   Set the render, which will add this graph object to itself.( for calculating size )[override]
         * @param   render: The FITKGrapgRender
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-29
         */
        void setFITKRender(Comp::FITKGraphRender* render) override;

        /**
         * @brief   Update the actor with the camera normal.[override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-29
         */
        void updateCameraNormal() override;

        /**
         * @brief   Simplified the graph object if necessary.[virtual][override]
         * @param   enabled: Enabled simplified flag
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-07
         */
        virtual void simplified(bool enabled) override;

        /**
         * @brief   If the graph object has relation with the given data object id.[virtual][override]
         * @param   dataObjId: The data object id
         * @return  Has relation
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual bool isRelatedTo(int dataObjId) override;

        /**
         * @brief   Get the fixed sized bounds of this graph object.[virtual][override]
         * @param   The bounds size
         * @return  If the graph object has fixed sized bounds
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-29
         */
        virtual bool getFixedBounds(double* bounds) override;

    protected:
        /**
         * @brief   Constructor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-01
         */
        FITKGraphObjectInteractionBase() = default;

        /**
         * @brief   Initialize.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void init();

        /**
         * @brief   Initialize the actors.[virtual]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void initActors();

        /**
         * @brief   Clear the model ids and the visibility hash.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual void clearModelsInfo() override;

        /**
         * @brief   Clear the data.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        virtual void clearData() override;

        /**
         * @brief   Generate the poly data for the interactions or constraint.[virtual]
         * @param   interactionsData: The interactions or constraint data object
         * @return  Is OK
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual bool generatePolyData(Interface::FITKInteractionLocation* interactionsData);

        /**
         * @brief   Generate the poly data for the location.[virtual]
         * @param   loc: The constraint data location object
         * @param   modelIds: The locations' model ids[quote]( For updating visibility. )
         * @param   subSetIds: The sub set ids[quote]
         * @param   ugrid: The location grid data
         * @return  Is OK
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-17
         */
        virtual bool generateLocPolyData(Interface::FITKBoundaryLocation* loc, QList<int>& modelIds, QList<int>& subSetIds, vtkUnstructuredGrid* ugrid);

        /**
         * @brief   Generate interactions or constraint actor.[virtual][override]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        virtual void generateGraph() override;

        /**
         * @brief   Get the visibility from the data object.[virtual][override]
         * @return  The visibility
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        virtual bool getDataVisibility() override;

        /**
         * @brief   Generate set or comp-set or surface grid data.
         * @param   setSurfGrid: The set or surface grid data
         * @param   loc: The location data
         * @param   modelIds: The locations's model ids[quote]( For updating visibility. )
         * @param   subSetIds: The sub set ids[quote]
         * @param   removeNormals: Need to remove the normals array[default]
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-24
         */
        void generateComponentGridData(vtkUnstructuredGrid* setSurfGrid, Interface::FITKBoundaryLocation* loc, QList<int>& modelIds, QList<int>& subSetIds, bool removeNormals = true);

    protected:
        /**
         * @brief   The input source of glyph 3d.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-22
         */
        FITKPolygonSource* m_polygonSource{ nullptr };

//#ifdef QT_DEBUG
        /**
         * @brief   The input source of glyph 3d for debug.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-06
         */
        vtkPointSource* m_pointSource{ nullptr };
//#endif

        /**
         * @brief   The glyph list.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        QList<vtkGlyph3D*> m_glyphList;

        /**
         * @brief   Fixed size filters
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-03-29
         */
        QList<vtkDistanceToCamera*> m_distToCameraList;

        /**
         * @brief   The mask points filters.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-10-17
         */
        QList<vtkMaskPoints*> m_maskPointsList;

        /**
         * @brief   The default resolution of input arrow source.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-06-18
         */
        int m_sourceResolution = 6;

        /**
         * @brief   The glyph size of the shape.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        double m_glyphSize = 10.;

        /**
         * @brief   The glyph size of the shape while the graph object is highlighting.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-06-18
         */
        double m_highlightGlyphSize = 60.;

        // For coupling lines.
        //@{
        /**
         * @brief   The points data for interactions or constraint lines.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        vtkPoints* m_points{ nullptr };

        /**
         * @brief   The grid data for interactions or constraint.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        vtkUnstructuredGrid* m_ugrid{ nullptr };

        /**
         * @brief   The model id data array.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkIntArray* m_modelIdArray{ nullptr };

        /**
         * @brief   The graph actor for interactions or constraint lines or glyphs.
         *          P.S. This actor sometimes will use the filter's output port as input
         *          connection, and sometimes, it will use the 'm_polyData' as the
         *          input data directly.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        FITKGraphActor* m_fActor{ nullptr };
        //@}

        // For highlight interactions or constraint faces and points.
        //@{
        /**
         * @brief   The grid data for coupling location 1.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        vtkUnstructuredGrid* m_gridLoc_1{ nullptr };

        /**
         * @brief   The grid data for interactions or constraint location 2.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        vtkUnstructuredGrid* m_gridLoc_2{ nullptr };

        /**
         * @brief   The location's set or surface id 1.( Set or surface )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        int m_locationCompId_1 = -1;

        /**
         * @brief   The location's set or surface id 2.( Set or surface )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-04-26
         */
        int m_locationCompId_2 = -1;

        /**
         * @brief   The sub set ids of the comp set or surface.( the location 1 of the interactions )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-18
         */
        QList<int> m_subSetIds_1;

        /**
         * @brief   The sub set ids of the comp set or surface.( the location 2 of the load )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-07-18
         */
        QList<int> m_subSetIds_2;

        /**
         * @brief   The selection node for extract mesh grid.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkSelectionNode* m_selectNode{ nullptr };

        /**
         * @brief   The selection for vtkSelectionNode.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkSelection* m_section{ nullptr };

        /**
         * @brief   The mesh grid extractor.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkExtractSelection* m_extractSelection{ nullptr };

        /**
         * @brief   The indice array of extract id.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        vtkIdTypeArray* m_idArray{ nullptr };

        /**
         * @brief   All model ids for surface of the location 1.
         *          ( Sometimes, if the location's count is over 2
         *          , then this list will store all locations' model id. )
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        QList<int> m_modelIds_1;

        /**
         * @brief   All model ids for surface of the location 2.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        QList<int> m_modelIds_2;

        /**
         * @brief   The visibility of all locations' parent models.
         * @author  ChengHaotian (yeguangbaozi@foxmail.com)
         * @date    2024-05-14
         */
        QHash<int, bool> m_modelVisibilityHash;

    };
}   // namespace Exchange

#endif // __FITKGRAPHOBJECTINTERACTIONBASE_H__
